package com.example.dayu521.gateway.filter;

import cn.dev33.satoken.reactor.filter.SaReactorFilter;
import cn.dev33.satoken.same.SaSameUtil;
import cn.dev33.satoken.stp.StpUtil;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import org.springframework.web.server.WebFilterChain;
import reactor.core.publisher.Mono;

@Component
public class AddLoginIdFilter implements GlobalFilter {

    public static final String Tpx="1122334455667788shangjia_login_id";
    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {

        var shangjia_login_id = (String)exchange.getAttributes().get(Tpx);

        if(shangjia_login_id==null){
            return chain.filter(exchange);
        }

        ServerHttpRequest newRequest = exchange
                .getRequest()
                .mutate()
                // 为请求追加 login_id 参数
                .header("login_id", shangjia_login_id)
                .build();
        //todo 清除Tpx
        ServerWebExchange newExchange = exchange.mutate().request(newRequest).build();
        return chain.filter(newExchange);
    }
}

